//
//  SNTopRankingViewController.swift
//  eyepetizer
//
//  Created by stone on 16/6/2.
//  Copyright © 2016年 stone. All rights reserved.
//

import UIKit
private let orderTableCellId = "orderTableCellId"

class BEMyOrdersListController: UIViewController {
    
    var ordersListViewModel: BEMyOrdersListViewModel = BEMyOrdersListViewModel()
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        if !BEAccountModel.isLogin(){
            let alertVC = UIAlertController(title: "Please login first!", message: "Log in before you can view the orders", preferredStyle: .alert)
            
            let cancleAction = UIAlertAction(title: "Later", style: .cancel) { (_) in
                if self.view.subviews.count == 0{
                    return
                }
                self.tableView.removeFromSuperview()
            }

            let doneAction = UIAlertAction(title: "Login Now", style: .default) { (_) in
                    _ = isLogin()
            }
            alertVC.addAction(cancleAction)
            alertVC.addAction(doneAction)
            
            UIApplication.shared.keyWindow?.rootViewController!.present(alertVC, animated: true, completion: nil)
            return
        }
        setupUI()
        loadData()
        self.tableView.reloadData()
    }
    
    private func setupUI(){
//        tableView.mj_header = setupHeaderRefresh(target: self, action: #selector(updateHomeData))
//        tableView.mj_header.beginRefreshing()
        view.backgroundColor = COLOR_ALL_BG
        view.addSubview(tableView)
    }
    
    @objc private func updateHomeData() {
        loadData()
    }
    
    private func loadData(){
        ordersListViewModel.ordersViewModelData.removeAll()
        JFProgressHUD.showWithStatus(status: "Loading...")
        showNetworkActivity()
        ordersListViewModel.loadOrderListData{ (success, tip) in
//            self.tableView.mj_header.endRefreshing()
            if success {
                JFProgressHUD.showSuccessWithStatus(status: tip)
                DispatchQueue.main.async(execute: { 
                    self.tableView.reloadData()
                })
            } else {
                JFProgressHUD.showInfoWithStatus(status: tip)
            }
        }
    }
    
    private lazy var tableView: UITableView = {
        let tableView = UITableView()
        tableView.frame = self.view.bounds
        tableView.backgroundColor = UIColor.colorWithHexString(hex: "f5f5f8")
        tableView.dataSource = self
        tableView.delegate = self
        
        tableView.register(UINib(nibName: "BEOrderTableCell", bundle: nil), forCellReuseIdentifier: orderTableCellId)
        
        tableView.rowHeight = 242
        //分割线
        tableView.separatorStyle = .none
        return tableView
    }()
}

extension BEMyOrdersListController: UITableViewDelegate,UITableViewDataSource{
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return ordersListViewModel.ordersViewModelData.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: orderTableCellId, for: indexPath as IndexPath) as! BEOrderTableCell
        cell.selectionStyle = .none
        cell.ordersViewModel = ordersListViewModel.ordersViewModelData[indexPath.row]
        return cell
    }
    
    func tableView(_ didSelectRowAttableView: UITableView, didSelectRowAt indexPath: IndexPath) {
/*
         orderStatu: UILabel!
         @IBOutlet weak var orderSn: UILabel!
         @IBOutlet weak var rigName: UILabel!
         @IBOutlet weak var rigLocation
         */
        
        let orderModel = ordersListViewModel.ordersViewModelData[indexPath.row]
      
        let orderDetailVC = BEOrderDetailController()
        navigationController?.pushViewController(orderDetailVC, animated: true)
        orderDetailVC.orderModel = orderModel
    }
}
